extends CharacterBody2D

signal plane_outside

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@export var max_tilt_angle:int = 45
@export var min_tilt_angle:int = -45
@export var click_power:int = 200
var frames_arr = [preload("res://tres/plane_blue.tres"), preload("res://tres/plane_green.tres"),preload("res://tres/plane_red.tres"), preload("res://tres/plane_yellow.tres")]
const PUFF_LARGE = preload("res://textures/Rocks/puffLarge.png")

func _ready():
	#$AnimatedSprite2D.sprite
	#.set_sprite_frames("res://plane_yellow.tres")
	$AnimatedSprite2D.sprite_frames = frames_arr[Global.plane_type]
	$AnimatedSprite2D.play()

func _physics_process(delta):
	velocity.y += gravity * delta
	var tilt_angle = clamp((velocity.y / gravity) * max_tilt_angle,min_tilt_angle,max_tilt_angle)
	rotation_degrees = tilt_angle
	move_and_slide()
	if position.y <=0 or position.y >= 480:
		plane_outside.emit()

func _unhandled_input(event):
	if(event.is_action_pressed("click")):
		velocity.y -= click_power
		$AudioStreamPlayer.play()
		show_puff()

# 显示喷气
func show_puff():
	var puff = Sprite2D.new()
	puff.set_texture(PUFF_LARGE)
	puff.scale = Vector2(1.5,1.5)
	add_child(puff)
	puff.position.x = -70
	var tween = get_tree().create_tween()
	tween.tween_property(puff,"scale",Vector2(),0.3)
	tween.parallel().tween_property(puff,"position",Vector2(-110,0),0.3)
	tween.tween_callback(puff.queue_free)
	pass
